﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SAICAPPWEB.Common
{
    /// <summary>
    /// 日志类
    /// </summary>
    public class LogHelper
    {
        #region Var
        private static readonly ILog Log = LogManager.GetLogger("SAICAPPWEB");
        private static readonly object Locker = new Object();

        #endregion


        #region Error

        /// <summary>
        /// 记录一个错误信息
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="e">异常对象</param>
        public static void Error(string message, Exception e)
        {
            try
            {
                lock (Locker)
                {
                    Log.Error(message, e);
                }
            }
            catch (Exception err)
            {
                throw new ApplicationException(String.Format("Logger.Error() failed on logging '{0}'.", message), err);
            }
        }

        /// <summary>
        /// 记录一个错误信息
        /// </summary>
        /// <param name="message">信息内容</param>
        public static void Error(string message)
        {
            try
            {
                lock (Locker)
                {
                    Log.Error(message);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(String.Format("Logger.Error() failed on logging '{0}'.", message), e);
            }
        }
        #endregion // Error

        #region Warning

        /// <summary>
        /// 记录一个警告信息
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="exception">异常对象</param>
        public static void Warning(string message, Exception exception)
        {
            try
            {
                lock (Locker)
                {

                    Log.Warn(message, exception);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(String.Format("Logger.Warn() failed on logging '{0}'.", message + "::" + exception.Message), e);
            }
        }

        /// <summary>
        /// 记录一个警告信息
        /// </summary>
        /// <param name="message">信息内容</param>
        public static void Warning(string message)
        {
            try
            {
                lock (Locker)
                {
                    Log.Warn(message);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(String.Format("Logger.Warn() failed on logging '{0}'.", message), e);
            }
        }

        #endregion // Warning

        #region Fatal

        /// <summary>
        /// 记录一个程序致命性错误
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="exception">异常对象</param>
        public static void Fatal(string message, Exception exception)
        {
            try
            {
                lock (Locker)
                {
                    Log.Fatal(message, exception);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(String.Format("Logger.Fatal() failed on logging '{0}'.", message + "::" + exception.Message), e);
            }
        }

        /// <summary>
        /// 记录一个程序致命性错误
        /// </summary>
        /// <param name="message">信息内容</param>
        public static void Fatal(string message)
        {
            try
            {
                lock (Locker)
                {

                    Log.Fatal(message);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(String.Format("Logger.Fatal() failed on logging '{0}'.", message), e);
            }
        }

        #endregion // Fatal

        #region Info

        /// <summary>
        /// 记录信息
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="exception">异常对象</param>
        public static void Info(string message, Exception exception)
        {
            try
            {
                lock (Locker)
                {
                    Log.Info(message, exception);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(String.Format("Logger.Info() failed on logging '{0}'.", message + "::" + exception.Message), e);
            }
        }
        /// <summary>
        /// 记录信息
        /// </summary>
        /// <param name="message">信息内容</param>
        public static void Info(object message)
        {
            try
            {
                lock (Locker)
                {
                    Log.Info(message);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(String.Format("Logger.Info() failed on logging '{0}'.", message), e);
            }
        }

        #endregion


        #region Debug

        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="exception">异常对象</param>
        public static void Debug(string message, Exception exception)
        {
            try
            {
                lock (Locker)
                {
                    Log.Debug(message, exception);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(String.Format("Logger.Debug() failed on logging '{0}'.", message + "::" + exception.Message), e);
            }
        }

        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="message">信息内容</param>
        public static void Debug(string message)
        {
            try
            {
                lock (Locker)
                {
                    Log.Debug(message);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(String.Format("Logger.Debug() failed on logging '{0}'.", message), e);
            }
        }

        #endregion
    }

}